#include "stdafx.h"
#include "SGPrismatoid.h"

using namespace std;

#pragma once

double M_PI = 3.14159265358979323846264338327;

CSGPrismatoid::CSGPrismatoid(double t1, double t2, double b1, double b2, double leg, unsigned fn, double sd, unsigned Ty)
{
	length = leg;
	rad_top1 = t1;
	rad_top2 = t2;
	rad_bottom1 = b1;
	rad_bottom2 = b2;
	face_no = fn;
	start_deg = sd*M_PI/180;
	Type = Ty;
	Compute();
}

void CSGPrismatoid::Compute()
{
	double rotate_deg = 360./face_no * M_PI/180.;

	for (unsigned i = 0; i < face_no; i++)
	{
		bottom.push_back(CGrVector(rad_bottom1*cos(rotate_deg*(double)i+start_deg),0.0,rad_bottom2*sin(rotate_deg*(double)i+start_deg)));
		top.push_back(CGrVector(rad_top1*cos(rotate_deg*(double)i+start_deg), length, rad_top2*sin(rotate_deg*(double)i+start_deg)));
	}

	if ( Type == 4)
	{
		for (unsigned i = 0; i < face_no; i++)
		{
			CGrVector up = (CGrVector(0.0,length,0.0)) - (bottom[(i+1)%face_no]);
			CGrVector bot = (bottom[i])-(bottom[(i+1)%face_no]);
			CGrVector normal = Cross3(bot, up);
			normal.Normalize3();
			faceNorm.push_back(normal);
		}

	}
	else
	{
		for (unsigned i = 0; i < face_no; i++)
		{
			CGrVector up = (top[(i+1)%face_no])-(bottom[(i+1)%face_no]);
			CGrVector bot = (bottom[i])-(bottom[(i+1)%face_no]);
			CGrVector normal = Cross3(bot, up);
			normal.Normalize3();
			faceNorm.push_back(normal);
		}
	}				
}

CSGPrismatoid::~CSGPrismatoid(void)
{
}


void CSGPrismatoid::Render()
{
	//0:DISJOIN, 1:JOIN,2:HEADER,3:FOOTER,4:CONE};
    switch(Type)
    {
    case 0:	//DISJOIN

		glBegin(GL_POLYGON);
		glNormal3d(0.0,1.0,0.0);
		for (int i = face_no-1; i >= 0 ; i--)glVertex3dv(top[i]);
		glEnd();


		glBegin(GL_POLYGON);
		glNormal3d(0.0,-1.0,0.0);
		for (unsigned i = 0; i <= face_no-1 ; i++)glVertex3dv(bottom[i]);
		glEnd();

		for (unsigned i = 0; i < face_no; i++)
		{
			glBegin(GL_POLYGON);
			glNormal3dv(faceNorm[i]);
			glVertex3dv(top[(i+1)%face_no]);
			glVertex3dv(bottom[(i+1)%face_no]);
			glVertex3dv(bottom[i]);
			glVertex3dv(top[i]);
			glEnd();
		}
		break;

    case 1://JOIN
 		for (unsigned i = 0; i < face_no; i++)
		{
			glBegin(GL_POLYGON);
			glNormal3dv(faceNorm[i]);
			glVertex3dv(top[(i+1)%face_no]);
			glVertex3dv(bottom[(i+1)%face_no]);
			glVertex3dv(bottom[i]);
			glVertex3dv(top[i]);
			glEnd();
		}
        break;
	
	case 2://HEADER
		glBegin(GL_POLYGON);
		glNormal3d(0.0,1.0,0.0);
		for (int i = face_no-1; i >= 0 ; i--)glVertex3dv(top[i]);
		glEnd();

		for (unsigned i = 0; i < face_no; i++)
		{
			glBegin(GL_POLYGON);
			glNormal3dv(faceNorm[i]);
			glVertex3dv(top[(i+1)%face_no]);
			glVertex3dv(bottom[(i+1)%face_no]);
			glVertex3dv(bottom[i]);
			glVertex3dv(top[i]);
			glEnd();
		}
		break;

	case 3://FOOTER

		glBegin(GL_POLYGON);
		glNormal3d(0.0,-1.0,0.0);
		for (unsigned i = 0; i <= face_no-1 ; i++) glVertex3dv(bottom[i]);
		glEnd();

		for (unsigned i = 0; i < face_no; i++)
		{
			glBegin(GL_POLYGON);
			glNormal3dv(faceNorm[i]);
			glVertex3dv(top[(i+1)%face_no]);
			glVertex3dv(bottom[(i+1)%face_no]);
			glVertex3dv(bottom[i]);
			glVertex3dv(top[i]);
			glEnd();
		}
		break;

	case 4://CONE
		glBegin(GL_POLYGON);
		glNormal3d(0.0,-1.0,0.0);
		for (unsigned i = 0; i <= face_no-1 ; i++)glVertex3dv(bottom[i]);
		glEnd();

		for (unsigned i = 0; i < face_no; i++)
		{
			glBegin(GL_POLYGON);
			glNormal3dv(faceNorm[i]);
			glVertex3dv(bottom[(i+1)%face_no]);
			glVertex3dv(bottom[i]);
			glVertex3dv(CGrVector(0.0,length,0.0));
			glEnd();
		}
		break;

	}
}